package com.gerfmarquez.nymph.ui.listeners;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gerfmarquez.nymph.ui.numberpicker.NumberPicker;
import com.gerfmarquez.nymph.ui.seekbar.SpawnNoteView;
import com.gerfmarquez.nymph.ui.seekbar.VerticalSeekBar;
import com.gerfmarquez.nymph.ui.seekbar.ViewInflater;

public class DialogListenerActionMusicNote implements DialogInterface.OnClickListener {
	Context context;
	VerticalSeekBar vsb;
	final DialogListenerActionMusicNote dlamn;
	int numberOfNotes;
	
	
	public DialogListenerActionMusicNote(Context context) {
		this.context = context;
		dlamn = this;
	}
	public void onClick(DialogInterface dialog, final int item) {
		final LinearLayout individualNote = (LinearLayout)vsb.getParent();
		final LinearLayout horizontalMainLayout = (LinearLayout)individualNote.getParent();
		final int indexChild = horizontalMainLayout.indexOfChild(individualNote);
//        Toast.makeText(context, items[item], Toast.LENGTH_SHORT).show();
		final AlertDialog.Builder builder = new AlertDialog.Builder(context);
		final LinearLayout numPickLay = ViewInflater.getNumberPicker(context);
		builder.setView(numPickLay);
		builder.setTitle("How many music notes to add or remove?");
		builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
//				NumberPicker np = (NumberPicker)numPickLay.getChildAt(0);
				TextView tv = (TextView)numPickLay.getChildAt(1);
				int numberOfNotes = Integer.parseInt(tv.getText().toString());
				dlamn.numberOfNotes = numberOfNotes;
				dlamn.doWorkAddOrRemoveNotes(individualNote, indexChild,  builder, item, horizontalMainLayout);
			}
		});
		AlertDialog alert = builder.create();
		alert.show();

    }
	public void setVerticalSeekBar(VerticalSeekBar vsb) {
		this.vsb = vsb;
	}
	private void doWorkAddOrRemoveNotes(LinearLayout individualNote,int indexChild,AlertDialog.Builder builder,
			int item, LinearLayout horizontalMainLayout) {
		for(int i = 0; i < dlamn.numberOfNotes; i++) {
			switch(item) {
			case 0://Add
	//			Add Music note view
				LinearLayout musicNoteLinearVerticalLayout = SpawnNoteView.spawnNoteView(context);
				LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
				((LinearLayout)individualNote.getParent()).addView(musicNoteLinearVerticalLayout, indexChild, layoutParams);
				Log.d("OnTouchListenerSeekBar", "Added view in index of parent = "+indexChild);
				indexChild++;
				break;
			case 1://Remove
	//			Remove music note view
//				horizontalMainLayout.removeView(individualNote);
				horizontalMainLayout.removeView((LinearLayout)horizontalMainLayout.getChildAt(indexChild));
				individualNote.setVisibility(View.GONE);
				Log.d("OnTouchListenerSeekBar", "Removed view with id = "+indexChild);
				indexChild--;
				break;
			case 2://other
				break;
			default:
				break;
			}
		}
	}
}
